package com.uton.rfidcontroller.view.activity;

import android.util.Log;

import com.cincc.cinvccbar.VccBarCtrl;
import com.cincc.cinvccbar.VccBarPublic;
import com.cincc.cinvccbar.acp.AcpEnumVector;
import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.GlobalConstants;
import com.uton.rfidcontroller.common.intent.IntentBuilder;
import com.uton.rfidcontroller.common.interfaces.IPublicController;
import com.uton.rfidcontroller.common.utils.SharedPreferencesUtils;
import com.uton.rfidcontroller.common.utils.Utils;
import com.uton.rfidcontroller.customize.SearchView;
import com.uton.rfidcontroller.customize.Tablayout;
import com.uton.rfidcontroller.databinding.ActivityMainBinding;
import com.uton.rfidcontroller.view.adapter.BankingContentAdapter;
import com.uton.rfidcontroller.view.base.BaseActivity;
import com.uton.rfidcontroller.view.fragment.MainLeftFragment;
import com.uton.rfidcontroller.view.fragment.MainRightFragment;
import com.uton.rfidcontroller.viewmodel.activity_model.MainViewModel;

import butterknife.BindView;
import io.reactivex.ObservableTransformer;

/**
 * Created by 王家宝 on 2017/9/1.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> {
    @BindView(R.id.sv)
    SearchView mSearchView;
    @BindView(R.id.tablayout)
    Tablayout mTablayout;

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void inject() {
        getBinding().setMainViewModel(getViewModel());
    }

    @Override
    protected String setTitle() {
        return getString(R.string.ui_title_test);
    }

    @Override
    protected void doSomething() {

        VccBarCtrl.instance().GetAttributeCollection().setMainIP("192.168.1.53");
        if (SharedPreferencesUtils.getToken().length() != GlobalConstants.EMPTY_TOKEN_LENGTH) {
            showRightSetting();
            mTablayout.initViewPager(new BankingContentAdapter(getSupportFragmentManager()),
                    new MainLeftFragment(), new MainRightFragment());
            mTablayout.setLeftTitle(R.string.ui_device);
            mTablayout.setRightTitle(R.string.ui_input);
            mTablayout.setTabExtraMode();
            mTablayout.setupWithSearchView(mSearchView);

            mSearchView.setSearchController(new IPublicController.ISearchController() {
                @Override
                public void doOnSearch(String key) {
                    Utils.showShortToast("搜索关键字是：" + key);
                }

                @Override
                public void doOnCancel(String emptyKey) {
                }
            });
        } else {
            startActivity(new IntentBuilder().from(this, LoginActivity.class).getIntent());
            finish();
        }

        initService();
        Log.d("MainActivity", "运行完毕");

    }

    @Override
    protected MainViewModel setViewModel() {
        return new MainViewModel() {
            @Override
            public ObservableTransformer setObservable() {
                return compose(bindToLifecycle());
            }
        };
    }

    public void initService(){
        VccBarCtrl.instance().GetAttributeCollection().setMainIP("192.168.20.122");
        VccBarCtrl.instance().GetAttributeCollection().setAgentID(String.format("000010%s%s", "111111", "5002"));
        VccBarCtrl.instance().GetAttributeCollection().setVccID("111111");
        VccBarCtrl.instance().GetAttributeCollection().setDn(String.format("000002%s%s", "111111", "5002"));
        VccBarCtrl.instance().GetAttributeCollection().setPassWord("111111");
        VccBarCtrl.instance().GetAttributeCollection().setAcpTraceFlag(1 + 2 + 4 + 8); //所有的ACP消息

        VccBarCtrl.instance().GetAttributeCollection().setSipServerIP("192.168.20.122");
        VccBarCtrl.instance().GetAttributeCollection().setSipServerPort(5066);
        VccBarCtrl.instance().GetAttributeCollection().setSipPassWord("Q6v^!t@HS1Cd?qF");

        VccBarCtrl.instance().GetAttributeCollection().setPhoneType(VccBarPublic.PhoneType_t.PhoneSip);
        VccBarCtrl.instance().GetAttributeCollection().setSipTraceFlag(3); //所有的SIP消息
        VccBarCtrl.instance().GetAttributeCollection().setLogFileEnable(1);
        VccBarCtrl.instance().GetAttributeCollection().setAutoAnswer(1);
        VccBarCtrl.instance().GetAttributeCollection().setTaskId("0");


        int mode = AcpEnumVector.SocketMode.SOCKETMODE_TCP;
        VccBarCtrl.instance().GetAttributeCollection().setTransMode(mode);
        if(mode == AcpEnumVector.SocketMode.SOCKETMODE_SOCKETIO){
            VccBarCtrl.instance().GetAttributeCollection().setMainPortID(14812);
        } else
            VccBarCtrl.instance().GetAttributeCollection().setMainPortID(14800);

    }

}
